package com.supersit.greensproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.readystatesoftware.viewbadger.BadgeView;
import com.supersit.greensproject.DB.XUtilsDbManager;
import com.supersit.greensproject.R;
import com.supersit.greensproject.entity.BaseBase;
import com.supersit.greensproject.entity.MaketSubject;
import com.supersit.greensproject.entity.PriceGather;
import com.supersit.greensproject.resources.Resources;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.List;
import java.util.Map;

/**
 * Created by cong on 2016/7/13.
 */
public class SubjectDataAdapter extends BaseAdapter {
    private Context context;
    private List<Map<Integer, String>> data;
    private LayoutInflater inflater;
    private DbManager db;
    private int hasMessige;
    private int num = 0;
//    private int[] baseAuthority;
//    private int[] maketAuthority;

    public SubjectDataAdapter(Context context, List<Map<Integer, String>> data, int hasMessige) {
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);
        db = db = XUtilsDbManager.getInstance();
        this.hasMessige = hasMessige;
//        baseAuthority = Authority.getBaseAuthority(context);
//        maketAuthority = Authority.getMaketAuthority(context);

    }

    @Override
    public int getCount() {
        if (data.size() < 1 || null == data) {
            return 0;
        } else {
            return data.size();
        }

    }

    @Override
    public Object getItem(int position) {
        if (data.size() < 1 || null == data) {
            return null;
        } else {
            return data.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        HolderView holderView;
        if (null == convertView) {
            holderView = new HolderView();
            convertView = inflater.inflate(R.layout.item_subject_data, null);
            holderView
                    .tvName = (TextView) convertView.findViewById(R.id.tv_item_name);
            holderView.rlRoot = (RelativeLayout) convertView.findViewById(R.id.rl_root);
            convertView.setTag(holderView);
        } else {
            holderView = (HolderView) convertView
                    .getTag();
        }

        if (hasMessige == Resources.HasMessige) {
            List<?> list = null;
            Map<Integer, String> item = data.get(position);
            for (int key : item.keySet()) {
                num = key;
            }
            if (num == 0) {
                holderView.tvName.setText(item.get(num));
                BadgeView badgeView = new BadgeView(context, holderView.rlRoot);
                try {
                    list = db.selector(BaseBase.class).where("isDelete", "=", 1).and("type", "=", num).and("operationsTatus", ">=", 0).findAll();
                    if (null != list && list.size() > 0) {
                        badgeView.setText(list.size() + "");
                        badgeView.show();
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
            } else if (num == 1) {
                holderView.tvName.setText(item.get(num));
                BadgeView badgeView = new BadgeView(context, holderView.rlRoot);
                try {
                    list = db.selector(BaseBase.class).where("isDelete", "=", 1).and("type", "=", num).and("operationsTatus", ">=", 0).findAll();
                    if (null != list && list.size() > 0) {
                        badgeView.setText(list.size() + "");
                        badgeView.show();
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
            } else if (num == 2) {
                holderView.tvName.setText(item.get(num));
                BadgeView badgeView = new BadgeView(context, holderView.rlRoot);
                try {
                    list = db.selector(BaseBase.class).where("isDelete", "=", 1).and("type", "=", num).and("operationsTatus", ">=", 0).findAll();
                    if (null != list && list.size() > 0) {
                        badgeView.setText(list.size() + "");
                        badgeView.show();
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
            } else if (num == 3) {
                holderView.tvName.setText(item.get(num));
                BadgeView badgeView = new BadgeView(context, holderView.rlRoot);
                try {
                    list = db.selector(BaseBase.class).where("isDelete", "=", 1).and("type", "=", num).and("operationsTatus", ">=", 0).findAll();
                    if (null != list && list.size() > 0) {
                        badgeView.setText(list.size() + "");
                        badgeView.show();
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
            } else if (num == 4) {
                holderView.tvName.setText(item.get(num));
                BadgeView badgeView = new BadgeView(context, holderView.rlRoot);
                try {
                    list = db.selector(BaseBase.class).where("isDelete", "=", 1).and("type", "=", num).and("operationsTatus", ">=", 0).findAll();
                    if (null != list && list.size() > 0) {
                        badgeView.setText(list.size() + "");
                        badgeView.show();
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
            } else if (num == 5) {
                holderView.tvName.setText(item.get(num));
                BadgeView badgeView = new BadgeView(context, holderView.rlRoot);
                try {
                    list = db.selector(MaketSubject.class).where("isDelete", "=", 1).and("operationsTatus", ">=", 0).findAll();
                    if (null != list && list.size() > 0) {
                        badgeView.setText(list.size() + "");
                        badgeView.show();
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
            } else if (num == 6) {
                holderView.tvName.setText(item.get(num));
                BadgeView badgeView = new BadgeView(context, holderView.rlRoot);

            } else if (num == 7) {
                holderView.tvName.setText(item.get(num));
                BadgeView badgeView = new BadgeView(context, holderView.rlRoot);
                try {
                    list = db.selector(PriceGather.class).where("isDelete", "=", 1).and("operationsTatus", ">=", 0).findAll();
                    if (null != list && list.size() > 0) {
                        badgeView.setText(list.size() + "");
                        badgeView.show();
                    }
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        } else {
            Map<Integer, String> item = data.get(position);
            for (int key : item.keySet()) {
                holderView.tvName.setText(item.get(key));
            }
        }
        return convertView;
    }

    class HolderView {
        TextView tvName;
        RelativeLayout rlRoot;
    }
}
